package cn.edu.ren.dao.imp;

import cn.edu.ren.dao.IPersonDao;
import cn.edu.ren.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @PackageName: cn.edu.ren.dao.imp
 * @ClassName: PersonDaoImp
 * @Description:
 * @author: RZS
 * @date: 2021/5/19  21:46
 */
@Repository
public class PersonDaoImp implements IPersonDao {

    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public List<Person> selectAll() {
        return jdbc.query("select * from user", new BeanPropertyRowMapper<>(Person.class));
    }

    @Override
    public Person selectById(Integer id) {
        return jdbc.queryForObject("select * from user where id = ?", new BeanPropertyRowMapper<>(Person.class), new Object[]{id});
    }

    @Override
    public void insert(Person person) {
        jdbc.update("insert into user(username, password, flag) values(?, ?, 1)", person.getUsername(), person.getPassword());
    }

    @Override
    public void updateUsernameById(String username, Integer id) {
        jdbc.update("update user set username=? where id=?", username, id);
    }

    @Override
    public void deleteById(Integer id) {
        jdbc.update("delete from user where id = ?", id);
    }
}
